package day02

object ScalaLazy {
  def main(args: Array[String]): Unit = {
    //创建一个List
    val list0 = List(2,7,3,4,5,9,8,0,1,6)
    //将list0中的每一个元素*2后生成一个新的集合
    val list1 = list0.map(_*2)
    println(list1)
    //将list0中的偶数取出来生成一个新的集合
    val list2 = list0.filter(_ % 2 ==0)
    println(list2)
    //将list排序后生成一个新的集合
    val list3 = list0.sorted
    println(list3)
    //反转排序顺序
    val list4 = list0.sorted.reverse
    println(list4)
    //将list0中的元素4个一组，类型为Iterator[List[Int]]
    val iterator = list0.grouped(4)
    println(iterator.toBuffer)
    //将Iterator换成List
    val list5 = iterator.toList
    println(list5)
    //将多个List压扁成一个List
    val flatten = list5.flatten
    println(flatten)
    //先按空格切分，在压平
    val list6 = List("hello java","hello scala","hello python","hello C++")
    val splitList = list6.map(_.split(" "))
    println(splitList.flatten)
    val splitLis1 = list6.flatMap(_.split(" "))
    println(splitLis1)

    //并行计算求和
    val arr = Array(1,2,3,4,5,6,7,8,9,10)
    val res = arr.sum
    var res_par = arr.par.sum
    println(res)
    println(res_par)
    //按照特定的顺序进行聚合
    val res_red: Int = arr.reduce(_+_)
    println(res_red)
    //折叠：有初始值（无特定顺序）
    val res_fold = arr.par.fold(10)(_+_)
    println(res_fold)
    //折叠：有初始值（有特定顺序）
    val res_nol = arr.fold(10)(_-_)
    val res_nol2 = arr.foldRight(10)(_-_)
    val res_nol3 = arr.foldLeft(10)(_-_)
    val res_nol4 = arr.par.foldLeft(10)(_-_)
    val res_nol5 = arr.par.foldRight(10)(_-_)
    println("------------------------------")
    println(res_nol)
    println(res_nol2)
    println(res_nol3)
    println(res_nol4)
    println(res_nol5)
    //聚合
    val listA = List(List(1,2,3),List(3,4,5),List(2),List(0))
    val resListA: Int = listA.flatten.reduce(_+_)
    println(resListA)
    val resAgg = listA.aggregate(0)(_+_.sum,_+_)
    println(resAgg)
    //--------------------------
    val l1 = List(5,6,4,7)
    val l2 = List(1,2,3,4)

    //求并集
    val res_union = l1 union l2
    println(res_union)

    //求交集
    val res_intersect = l1 intersect l2
    println(res_intersect)

    //求差集
    val res_diff = l1 diff l2
    println(res_diff)
  }
}
